package com.free.json;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JSONObjectTest {


	public static void main(String[] args) {
//		 json2object();
//		 object2json();
//		 map2json();
//		list2json();
		
	}

	static void object2json() {
		Address address = new Address();
		address.setNum("104");
		address.setProvience("陕西");
		address.setRoad("高新路");
		address.setStreate("");
		JSONArray json = JSONArray.fromObject(address);
		System.out.println(json.toString());
	}

	static void map2json() {
		Address address = new Address();
		address.setNum("104");
		address.setProvience("陕西");
		address.setRoad("高新路");
		address.setStreate("");
		Address address2 = new Address();
		address2.setNum("105");
		address2.setProvience("陕西");
		address2.setRoad("未央路");
		address2.setStreate("张办");
		List<Address> list = new ArrayList<Address>();
		list.add(address);
		list.add(address2);
//		JSONArray json = JSONArray.fromObject(list);
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("ok", "false");
		map.put("root", list);
		JSONArray json = JSONArray.fromObject(map);
		System.out.println(json.toString());
	}

	static void list2json() {
		Address address = new Address();
		address.setNum("104");
		address.setProvience("陕西");
		address.setRoad("高新路");
		address.setStreate("");
		Address address2 = new Address();
		address2.setNum("105");
		address2.setProvience("陕西");
		address2.setRoad("未央路");
		address2.setStreate("张办");
		List<Address> list = new ArrayList<Address>();
		list.add(address);
		list.add(address2);
		JSONArray json = JSONArray.fromObject(list);
		
		System.out.println(json.toString());
	}
	static void json2object() {

		JSONObject jsonObject = JSONObject
				.fromObject("{\"num\":\"104\",\"provience\":\"陕西\",\"road\":\"高新路\",\"streate\":\"\"}");
		Address address = (Address) JSONObject
				.toBean(jsonObject, Address.class);
		
		System.out.println(address.getNum());
		System.out.println(address.getProvience());
		System.out.println(address.getRoad());
		System.out.println(jsonObject.get("num"));
	}

}
